package com.unibert.podcastq;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.Toast;

public class SUBSCRIBE extends Activity {

	ArrayList<String> Lista ;
	Spinner spin;
	ImageButton send;
	EditText nombre, email, telefono, ciudad;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_subscribe);
//
//		spin = (Spinner) findViewById(R.id.combo);
		send = (ImageButton) findViewById(R.id.Sendnau);
		nombre = (EditText) findViewById(R.id.nombreedit);
		email = (EditText) findViewById(R.id.mailedit);
		telefono = (EditText) findViewById(R.id.telefonoedit);
		ciudad = (EditText) findViewById(R.id.ciudadedit);
		
		send.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {

				if(email.getText().length()>0&&nombre.getText().length()>1&&ciudad.getText().length()>0){
					new MeterseEnGrupos().execute();
				}
				else{
					Toast.makeText(SUBSCRIBE.this, "Rellena los campos obligatorios", Toast.LENGTH_LONG).show();
				}
				
			}
		});
		
		
		
		//new ObtenerGrupos().execute(null);
	}

	private class MeterseEnGrupos extends AsyncTask<String, String, String> {
		JSONObject recep;
		ProgressDialog dialog;
		@Override
		protected void onPostExecute(String result) {
			dialog.dismiss();
			Toast.makeText(SUBSCRIBE.this, "PROCESO COMPLETADO", Toast.LENGTH_SHORT);
			SUBSCRIBE.this.onBackPressed();
		}
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			try {
				dialog = new ProgressDialog(SUBSCRIBE.this);
				dialog.setTitle(SUBSCRIBE.this.getString(R.string.cargando));
				dialog.setMessage(SUBSCRIBE.this.getString(R.string.obtInfoServer));
				dialog.setCancelable(false);
				dialog.show();
			} catch (final Exception e) {
				Log.e("Error", e.getMessage());
			}

		}
		@Override
		protected String doInBackground(String... params) {
//			&merge_vars[GROUPINGS][0][id]=6633&merge_vars[GROUPINGS][0][groups]="+params[2]
//			
//qapp.cristianosuniversitarios.org/interesados.php?nombre=Nacho&correo=nmvlc@aim.com&telefono=123&ciudad=Valencia&origen=qapp
			try {
				URL url = new  URL("http://qapp.cristianosuniversitarios.org/interesados.php?" +
						"nombre="+nombre.getText()+"&" +
						"correo="+email.getText()+"&" +
						"telefono="+telefono.getText()+"&" +
						"ciudad="+ciudad.getText()+"&" +
						"origen=qapp");
//				URL url = new  URL("http://us6.api.mailchimp.com/1.3/?method=listSubscribe&apikey=847861beb144132d7491f43c032cc9af-us6&id=9ad5f6a5f4&email_address="+email.getText()+"&merge_vars[FNAME]="+nombre.getText()+"&merge_vars[PHONE]="+telefono.getText()+"&merge_vars[EMAIL]="+email.getText()+"&double_optin=false" );
				Lista = new ArrayList<String>();
				InputStream is = url.openStream();
				is.close();


			} catch (Exception e) {
				// TODO Auto-generated catch block
				return "KO";
			}
			return "OK";
		}

	}


}


